from django.shortcuts import render

print('asdfasdf')
from .models import Banner
from .serializer import BannerSerializer
from utils.response import APIResponse
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin,UpdateModelMixin
from django.conf import settings
from django.core.cache import cache
class BannerView(GenericViewSet,ListModelMixin,UpdateModelMixin):
# class BannerView(GenericViewSet,ListModelMixin):
    # 获取所有接口-list，自动生成路由
    # qs对象可以像列表一样，切片
    queryset = Banner.objects.filter(is_delete=False,is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    serializer_class =BannerSerializer

    def list(self, request, *args, **kwargs): # 重写list
        # 逻辑是，先去redis中查询，如果有，直接返回，如果没有，再执行下面super().list这句，这句是去数据库中查
        banner_list=cache.get('banner_list')
        if banner_list:
            print('走了缓存，很快很快')
            return APIResponse(result=banner_list)
        else:
            print('没走缓存比较慢')
            res=super().list(request, *args, **kwargs)
            # 再缓存一份
            cache.set('banner_list',res.data)
            return APIResponse(result=res.data)
    def put(self, request, *args, **kwargs):
        return APIResponse()